package com.burgstaller.aGTD;

import com.burgstaller.provider.AGtd;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SettingsEditor extends Activity {

	SharedPreferences m_preferences;
	EditText m_userBox;
	EditText m_passBox;
	EditText m_serverBox;
	SharedPreferences.Editor m_prefsEditor;
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.settings);
		setTitle(R.string.header_settings);

		m_preferences = getSharedPreferences(
				AGtd.SETTINGS_NAME, Context.MODE_PRIVATE);
		m_prefsEditor = m_preferences.edit();
		m_serverBox = (EditText) findViewById(R.id.imapServer);
		m_userBox = (EditText) findViewById(R.id.imapUser);
		m_passBox = (EditText) findViewById(R.id.imapPass);
		m_serverBox.setText(m_preferences.getString(AGtd.Settings.IMAP_SERVER,
				""));
		m_userBox.setText(m_preferences.getString(AGtd.Settings.IMAP_USER, ""));
		m_passBox.setText(m_preferences.getString(AGtd.Settings.IMAP_PASS, ""));
		Button save = (Button) findViewById(R.id.save_button);
		save.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				updateFromControls();
				m_prefsEditor.commit();
				finish();
			}
		});
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		updateFromControls();
	}

	private void updateFromControls() {
		m_prefsEditor.putString(AGtd.Settings.IMAP_SERVER, 
				m_serverBox.getText().toString());
		m_prefsEditor.putString(AGtd.Settings.IMAP_USER, 
				m_userBox.getText().toString());
		m_prefsEditor.putString(AGtd.Settings.IMAP_PASS,
				m_passBox.getText().toString());
	}
}
